package com.yuantu.judiciary.dao;

import com.yuantu.judiciary.model.domain.OrgVipRecordDO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.sql.Timestamp;
import java.util.List;

/**
 * 组织套餐记录
 *
 * @author syw
 * @since 2023-08-14
 **/
public interface IOrgVipRecordDao extends JpaRepository<OrgVipRecordDO, Long> {

    OrgVipRecordDO findRecordByOrganizationIdAndDeletedFalse(Long organizationId);

    OrgVipRecordDO findRecordByIdAndDeletedFalse(Long id);

    @Modifying
    @Query("update OrgVipRecordDO set deleted = true, modifierId = :accountId, modifyTime = :currentTime " +
            "where ((coalesce (:id) is null and 1=0) or (coalesce (:id) is not null and id in (:id))) " +
            "and deleted = false ")
    void deleteByIdIn(List<Long> id, Long accountId, Timestamp currentTime);

    Page<OrgVipRecordDO> findByOrganizationIdInAndDeletedFalse(List<Long> organizationId, Pageable pageable);

    @Query("select distinct organizationId from OrgVipRecordDO where deleted = false")
    List<Long> findRecordByDeletedFalse();

}
